package com.hkd.common.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.hkd.common.core.constants.Constants;
import com.hkd.common.core.utils.ThreadLocalUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {


        Long userId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        if(userId!=null){
            this.strictInsertFill(metaObject, "createBy", Long.class, userId);
        }else {
            log.info("userId:{}",userId);
            //用户自己创建 5401L
            this.strictInsertFill(metaObject, "createBy", Long.class, 5401L);
        }


    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long userId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updateBy", Long.class, userId);
    }
}
